package com.etime.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/surveyServlet")
@MultipartConfig
public class SurveyServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    Part part = req.getPart("photo");
    String name = req.getParameter("name");
    String gender = req.getParameter("gender");
    String[] likes = req.getParameterValues("like");
    String path = "D://survey";
    File file = new File(path);
    if (!file.exists()){
      file.mkdirs();
    }
    String photo = part.getSubmittedFileName();
    part.write(path+"/"+photo);
    System.out.println(name+","+gender+","+ Arrays.toString(likes));
    resp.sendRedirect("index.html");
  }
}
